﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Text;

namespace testreportmanager
{
    public partial class _Default : System.Web.UI.Page
    {
        public int changed = 0;
        public int chkf = 0;        
        public string listname;
        private static string source;
        public static string Source
        {
            get { return source; }
        }
        

        protected void Page_Load(object sender, EventArgs e)
        {
            formatting();            
        }

        protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            listname = (String)TestListGrid.DataKeys[TestListGrid.SelectedIndex].Value;
            XmlDataSource2.DataFile = source;
            if (chkf == 1)
            {
                XmlDataSource2.XPath = String.Format("/TestRun/Results/*[@testListId='{0}' and @outcome='Failed']", listname);
            }
            else
            {
                XmlDataSource2.XPath = String.Format("/TestRun/Results/*[@testListId='{0}']", listname);
            }

            formatting();
            CommentsGrid.Visible = false;
            
        }
     
        private void formatting()
        {
            for (int i = 0; i < ResultsGrid.Rows.Count; i++)
            {

                if (ResultsGrid.Rows[i].Cells[1].Text == "Failed")
                {
                    ResultsGrid.Rows[i].ForeColor = System.Drawing.Color.Red;
                }
                else if (ResultsGrid.Rows[i].Cells[1].Text == "Passed")
                {
                    ResultsGrid.Rows[i].ForeColor = System.Drawing.Color.Green;
                }
            }
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string testid = (String)ResultsGrid.DataKeys[ResultsGrid.SelectedIndex].Value;
            XmlDataSource4.DataFile = source;
            XmlDataSource4.TransformFile = "comments.xsl";

            XmlDataSource4.XPath = String.Format("/Results/*[@testId='{0}']", testid);
            CommentsGrid.Visible = true;
        }              
               
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            formatting();           
        }

        private string GetPath(string fileName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("files.xml"));
            XmlNode file = doc.SelectSingleNode(String.Format("//file[@name='{0}']", fileName));
            StringBuilder path = new StringBuilder();

            if (file != null)
            {
                while (null != file.ParentNode)
                {
                    path.AppendFormat(",{0}", file.Attributes["fname"].Value);
                    file = file.ParentNode;
                }

                String[] temp = path.ToString().Split(',');
                StringBuilder final = new StringBuilder();
                for (int i = temp.Length - 1; i >= 0; i--)
                {
                    final.AppendFormat("/{0}", temp[i]);
                }
                string re = final.ToString().Remove(final.ToString().Length - 1);

                return Server.MapPath(re);
            }           

            return null;
        }

        protected void failedlnk_Click(object sender, EventArgs e)
        {
            XmlDataSource2.DataFile = source;
            XmlDataSource2.XPath = "/TestRun/Results/*[@outcome='Failed']";
            chkf = 1;
            formatting();
            CommentsGrid.Visible = false;
        }

        protected void totallnk_Click(object sender, EventArgs e)
        {
            XmlDataSource2.DataFile = source;
            XmlDataSource2.XPath = "/TestRun/Results/*";
            chkf = 0;
            formatting();
            CommentsGrid.Visible = false;
        }

        protected void bSearch_Click(object sender, EventArgs e)
        {
            XmlDataSource5.XPath = String.Format("/folder/file[@feature='{0}' and @build='{1}']", featurelist.SelectedItem.Text, buildlist.SelectedItem.Text);
            filesgrid.Visible = true;
            Summary.Visible = false;
            ResultsGrid.Visible = false;
            TestListGrid.Visible = false;
            CommentsGrid.Visible = false;            
        }

        protected void lbFname_Click(object sender, EventArgs e)
        {
            try
            {
                source = GetPath(((LinkButton)(sender)).Text);
                XmlDataSource1.DataFile = source;
                XmlDataSource2.DataFile = source;                
                XmlDataSource3.DataFile = source;
                XmlDataSource1.DataBind();
                XmlDataSource2.DataBind();                
                XmlDataSource3.DataBind();
                Summary.Visible = true;
                ResultsGrid.Visible = true;
                TestListGrid.Visible = true;
                CommentsGrid.Visible = false;
            }
            catch
            {   
                XmlDataSource1.DataFile = source;
                XmlDataSource2.DataFile = source;                
                XmlDataSource3.DataFile = source;
                XmlDataSource1.DataBind();
                XmlDataSource2.DataBind();                
                XmlDataSource3.DataBind();
                Summary.Visible = true;
                ResultsGrid.Visible = true;
                TestListGrid.Visible = true;
                CommentsGrid.Visible = false;
            }
            
            formatting();
        }

        protected void featurelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            string feature = featurelist.SelectedItem.Text;

            XmlDataSource8.XPath = String.Format(
                "/folder/file[(not(@build = following-sibling::file/@build and @build = preceding-sibling::file/@build) or @build) and @feature='{0}']"
                , feature);            
        }
        
        
    }
}
